import { createLocation } from "./createBrowserHistory";
import { Manager } from "./TransitionManager";
import { hasBasename, POP, stripBasename } from "./utils";


export function createHashHistory(options = {}) {
    if (!!!(typeof window !== 'undefined' && window.document && window.document.createElement)) {
        console.error("Hash history needs a DOM")
    }
    // 解构参数配置项并且赋默认值
    const {
        basename = "",
        hashType = "slash",
        getUserConfirmation = (message, callback) => callback(window.confirm(message))
    } = options;

    // 监听器以及阻塞模块
    const transitionManager = new Manager(getUserConfirmation);
    const location = getDOMLocation();
    function getDOMLocation() {
        // const path = decodePath(getHashPath());
        // if (!basename || hasBasename(path, basename)) {
        //     console.warn('You are attempting to use a basename on a page whose URL path does not begin with the basename. ')
        // }
        // if (basename) path = stripBasename(path, basename);
        // return createLocation(path);
    }
    function block(prompt) {
        // if (prompt === void 0) {
        //     prompt = false;
        // }

        // var unblock = transitionManager.setPrompt(prompt);

        // if (!isBlocked) {
        //     checkDOMListeners(1);
        //     isBlocked = true;
        // }

        // return function () {
        //     if (isBlocked) {
        //         isBlocked = false;
        //         checkDOMListeners(-1);
        //     }

        //     return unblock();
        // };
    }
    function createHref(location) {
        // return '#' + encodePath(basename + createPath(location));
    }
    function go(n) {
        window.history.go(n);
    }
    function goBack() {
        window.history.back();
    }
    function goForward() {
        window.history.forward();
    }
    function listen(listener) {
        var unlisten = transitionManager.appendListener(listener);
        // checkDOMListeners(1);
        return function () {
            //   checkDOMListeners(-1);
            unlisten();
        };
    }
    function push(path, state) {
        // warning(state === undefined, 'Hash history cannot push state; it is ignored');
        // var action = 'PUSH';
        // var location = createLocation(path, undefined, undefined, history.location);
        // transitionManager.confirmTransitionTo(location, action, getUserConfirmation, function (ok) {
        //     if (!ok) return;
        //     var path = createPath(location);
        //     var encodedPath = encodePath(basename + path);
        //     var hashChanged = getHashPath() !== encodedPath;

        //     if (hashChanged) {
        //         // We cannot tell if a hashchange was caused by a PUSH, so we'd
        //         // rather setState here and ignore the hashchange. The caveat here
        //         // is that other hash histories in the page will consider it a POP.
        //         ignorePath = path;
        //         pushHashPath(encodedPath);
        //         var prevIndex = allPaths.lastIndexOf(createPath(history.location));
        //         var nextPaths = allPaths.slice(0, prevIndex === -1 ? 0 : prevIndex + 1);
        //         nextPaths.push(path);
        //         allPaths = nextPaths;
        //         setState({
        //             action: action,
        //             location: location
        //         });
        //     } else {
        //         warning(false, 'Hash history cannot PUSH the same path; a new entry will not be added to the history stack');
        //         setState();
        //     }
        // });
    }
    function replace(path, state) {
        // warning(state === undefined, 'Hash history cannot replace state; it is ignored');
        // var action = 'REPLACE';
        // var location = createLocation(path, undefined, undefined, history.location);
        // transitionManager.confirmTransitionTo(location, action, getUserConfirmation, function (ok) {
        //   if (!ok) return;
        //   var path = createPath(location);
        //   var encodedPath = encodePath(basename + path);
        //   var hashChanged = getHashPath() !== encodedPath;

        //   if (hashChanged) {
        //     // We cannot tell if a hashchange was caused by a REPLACE, so we'd
        //     // rather setState here and ignore the hashchange. The caveat here
        //     // is that other hash histories in the page will consider it a POP.
        //     ignorePath = path;
        //     replaceHashPath(encodedPath);
        //   }

        //   var prevIndex = allPaths.indexOf(createPath(history.location));
        //   if (prevIndex !== -1) allPaths[prevIndex] = path;
        //   setState({
        //     action: action,
        //     location: location
        //   });
        // });
    }
    const history = {
        action: POP,
        block,
        createHref,
        go,
        goBack,
        goForward,
        length: window.history.length,
        listen,
        location,
        push,
        replace,
    };
    return history;
}